package com.AAL.DAO;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.ICityDAO;
import com.AAL.Model.City;
import com.AAL.Utils.HibernateUtil;

public class CityDAO implements ICityDAO {

	@SuppressWarnings("unchecked")
	@Override
	public List<City> getAllLstCity() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<City> lstCities = null;
		try {

			lstCities = (List<City>) session.createQuery("from City").list();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return lstCities;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<City> getLstCityByRegionID(int regionID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<City> lstCities = null;
		try {						
			lstCities = (List<City>) session
					.createQuery("from City as c where c.region.regionID=:regionID")
					.setParameter("regionID",(short) regionID).list();
			System.out.println("ds city:"+lstCities);
		} catch (HibernateException e) {
			System.out.println("Error:"+ e.getMessage());
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return lstCities;
	}

	@Override
	public City getCity(int cityID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		City city = (City) session.load(City.class, cityID);
		return city;
	}

}
